package com.qk.service.impl;

import com.github.pagehelper.Page;
import com.github.pagehelper.PageHelper;
import com.qk.common.PageResult;
import com.qk.entity.Dept;
import com.qk.mapper.DeptMapper;
import com.qk.service.DeptService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.time.LocalDateTime;
import java.util.List;

@Service
public class DeptServiceimpl implements DeptService {
    @Autowired DeptMapper deptMapper;

    @Override
    public void addDept(Dept dept) {
        dept.setCreateTime(LocalDateTime.now());
        dept.setUpdateTime(LocalDateTime.now());
        deptMapper.insert(dept);
    }

    @Override
    public PageResult<Dept> listDepts(String name, Integer status, Integer page, Integer pageSize) {
        PageHelper.startPage(page,pageSize);
        List<Dept> deptlist = deptMapper.selectlist(name,status);
        Page<Dept> p=(Page<Dept>) deptlist;
        return new PageResult<>(p.getTotal(),p.getResult());
    }

    @Override
    public Dept getById(Integer id) {
        Dept dept = deptMapper.selectByid(id);
        return dept;

    }

    @Override
    public void update(Dept dept) {
        dept.setUpdateTime(LocalDateTime.now());
        deptMapper.updatedept(dept);
    }

    @Override
    public void deletedept(Integer id) {
         deptMapper.deleteByid(id);
    }

    @Override
    public List<Dept> listBystatus() {
        List<Dept> list = deptMapper.listtostatus();
        return list;
    }

}
